﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace HovisPresent
{
	public partial class RunForm : UsefulForm
	{

		private Control[] networkControlsList;
		private Control[] secondMonitorControlList;
		public RunPresentationOptions Options { get; set; }
		
		public RunForm() {
			InitializeComponent();
			Options = new RunPresentationOptions();
		}

		/// <summary>
		/// Handles the Load event of the RunForm control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void RunForm_Load(object sender, EventArgs e) {
			networkControlsList = new Control[] { slaves };
			secondMonitorControlList = new Control[] {	monitor1IsLabel, dualMonitorOptionsLabel, 
														dualMonitorPresentationView, dualMonitorPresenterView };

			UpdateUIFromOptions();

			UpdateNetworkControls();
			EnableControls(secondMonitorControlList, Screen.AllScreens.Length > 1);

			string hostname = System.Net.Dns.GetHostName();
			string textHost = hostname;
			IPHostEntry ipEntry = Dns.GetHostByName(hostname);
			IPAddress [] addr = ipEntry.AddressList;
          
			for (int i = 0; i < addr.Length; i++)
				textHost+="/"+addr[i].ToString();

			localHostname.Text = textHost;
		}

		/// <summary>
		/// The network type has changed by the user
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void networkTypeChanged(object sender, EventArgs e) {
			UpdateNetworkControls();
		}

		/// <summary>
		/// Updates the network controls enable/disable
		/// </summary>
		private void UpdateNetworkControls() {
			EnableControls(networkControlsList, networkMasterOrLocal.Checked == true);
		}

		/// <summary>
		/// Handles the Click event of the run control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void run_Click(object sender, EventArgs e) {
			Options.NetworkMode = networkMasterOrLocal.Checked ? NetworkMode.LocalOrMaster : NetworkMode.Slave;
			Options.NetworkPort = (int) networkPort.Value;
			string[] slaveItems = slaves.Text.Trim().Split( new string[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
			Options.Slaves = new List<string>(slaveItems);

			// default, windowed mode
			Options.PresentationScreen = Options.PresenterScreen = Monitor.Monitor0;

			if (singleMonitorPresentationView.Checked)
				Options.PresenterScreen = Monitor.noMonitor;
			else if (singleMonitorPresenterView.Checked)
				Options.PresentationScreen = Monitor.noMonitor;
			else if (dualMonitorPresentationView.Checked)
				Options.PresentationScreen = Monitor.Monitor1;
			else if (dualMonitorPresenterView.Checked)
				Options.PresenterScreen = Monitor.Monitor1;
		}

		/// <summary>
		/// Update the controls to reflect the Options object;
		/// </summary>
		private void UpdateUIFromOptions() {
			networkPort.Value = Options.NetworkPort;
			networkMasterOrLocal.Checked = Options.NetworkMode == NetworkMode.LocalOrMaster;
			networkSlave.Checked = Options.NetworkMode == NetworkMode.Slave;

			// limit monitor choices (i.e. last run had multi screen, this doesn't)
			Options.PresentationScreen = (Monitor) Math.Min( (int) Options.PresentationScreen, Screen.AllScreens.Length - 1);
			Options.PresenterScreen = (Monitor) Math.Min( (int) Options.PresenterScreen, Screen.AllScreens.Length - 1);

			if (Options.PresentationScreen == Monitor.Monitor0 && Options.PresenterScreen == Monitor.Monitor0)
				singleMonitorWindowedView.Checked = true;
			else if (Options.PresentationScreen == Monitor.Monitor0 && Options.PresenterScreen == Monitor.noMonitor)
				singleMonitorPresentationView.Checked = true;
			else if (Options.PresentationScreen == Monitor.noMonitor && Options.PresenterScreen == Monitor.Monitor0)
				singleMonitorPresenterView.Checked = true;
			else if (Options.PresentationScreen == Monitor.Monitor0 && Options.PresenterScreen == Monitor.Monitor1)
				dualMonitorPresenterView.Checked = true;
			else if (Options.PresentationScreen == Monitor.Monitor1 && Options.PresenterScreen == Monitor.Monitor0)
				dualMonitorPresentationView.Checked = true;
			else // --- 
				singleMonitorPresentationView.Checked = true;

			slaves.Text = "";
			foreach (string s in Options.Slaves) {
				if (slaves.Text != "")
					slaves.Text += "\r\n";
				slaves.Text += s;
			}
		}

	}
}
